<template>
  <div class='sub-category'>
    <div class="container">
      <!-- 面包屑 -->
      <SubBread />
      <!-- 筛选区 -->
      <SubFilter @filter-change="changeFilter" />
      <!-- 结果区域 -->
      <div class="goods-list">
        <!-- 排序 -->
        <SubSort @sort-change="changeSort" />
        <!-- 商品列表 -->
        <ul v-if="goods">
          <li v-for="good in goods" :key="good.id">
            <GoodsItem :good="good" />
          </li>
        </ul>
        <XtxInfiniteLoading @infinite="getGoods" :loading="loading" :finished="finished"></XtxInfiniteLoading>
      </div>
    </div>
  </div>
</template>

<script>
import SubBread from '../components/sub-bread.vue'
import SubFilter from '../components/sub-filter'
import SubSort from '../components/sub-sort'
import GoodsItem from '../components/goods-item'
import { ref, watch } from 'vue'
import { useRoute } from 'vue-router'
import { findSubCategoryGoods } from '@/api/category'
export default {
  components: {
    SubBread,
    SubFilter,
    SubSort,
    GoodsItem
  },
  setup() {
    const route = useRoute()
    // 定义loading和finished
    const loading = ref(false)
    const finished = ref(false)
    // 定义筛选出的商品数据
    const goods = ref([])
    // 筛选数据的条件
    let reqParams = {
      categoryId: route.params.id,
      page: 1,
      pageSize: 20
    }
    // 获取分页数据
    const getGoods = () => {
      // 先将loading设置为true,正在加载中，不要再发起请求
      loading.value = true
      findSubCategoryGoods(reqParams).then(({ result }) => {
        if (result.items && result.items.length > 0) {
          // 赋值
          goods.value = [...goods.value, ...result.items]
          // goods.value.push(...result.item)
          // 页码+1
          reqParams.page++
        } else {
          // 数据已经是最后一页，没有其他数据
          finished.value = true
        }
        // 数据加载完成后，将loadding改成 false
        loading.value = false
      })
    }
    // 二级分类id发生变化时也要发起请求
    watch(() => route.params.id, (newVal) => {
      if (newVal && route.path === `/cate/sub/${route.params.id}`) {
        // 将数据置空,那么无限加载组件就占据原来商品的位置，就会发起网络请求
        goods.value = []
        reqParams = {
          page: 1,
          pageSize: 20,
          categoryId: route.params.id
        }
        // 避免上一个分类的请求已经是最后一页数据了
        finished.value = false
      }
    })

    // 排序组件的筛选
    const changeSort = (sortParams) => {
      reqParams.page = 1
      reqParams = { ...reqParams, ...sortParams }
      finished.value = false
      // 如果上一次请求就已经没有数据了，此时就需要自己主动触发获取数据的方法来重新发起网络请求，否则不会去触发下拉加载更多的组件去自动触发
      if (goods.value.length === 0) {
        getGoods()
      } else {
        goods.value = []
      }
    }
    // 品牌、属性条件的筛选
    const changeFilter = (filterData) => {
      reqParams.page = 1
      reqParams = { ...reqParams, ...filterData }
      finished.value = false
      if (goods.value.length === 0) {
        getGoods()
      } else {
        goods.value = []
      }
    }

    return { loading, finished, getGoods, goods, changeSort, changeFilter }
  }
}
</script>

<style lang="less" scoped>
.goods-list {
  background: #fff;
  padding: 0 25px;
  margin-top: 25px;

  ul {
    display: flex;
    flex-wrap: wrap;
    padding: 0 5px;

    li {
      margin-right: 20px;
      margin-bottom: 20px;

      &:nth-child(5n) {
        margin-right: 0;
      }
    }
  }
}
</style>